package com.example.thirdhomework

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class FifthActivity : AppCompatActivity() {

    var seconds = 0 // 计时器的秒数
    var running = false // 计时器是否在运行
    var wasRunning = false // 计时器之前是否在运行

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge() // 启用边缘到边缘显示
        setContentView(R.layout.activity_fifth) // 设置布局文件
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) // 设置系统栏的内边距
            insets
        }

        Log.d("life cycle", "onCreate") // 打印生命周期日志
        if(savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds") // 恢复秒数
            running = savedInstanceState.getBoolean("running") // 恢复运行状态
            wasRunning = savedInstanceState.getBoolean("wasRunning") // 恢复之前的运行状态
        }

        runTimer() // 启动计时器

        val button_start: Button = findViewById(R.id.button_start) // 获取开始按钮
        val button_stop: Button = findViewById(R.id.button_stop) // 获取停止按钮
        val button_restart: Button = findViewById(R.id.button_restart) // 获取重启按钮
        button_start.setOnClickListener {
            running = true // 开始计时
        }
        button_stop.setOnClickListener {
            running = false // 停止计时
        }
        button_restart.setOnClickListener {
            running = true // 重启计时
            seconds = 0 // 重置秒数
        }
    }

    private fun runTimer() {
        val handler = Handler() // 创建Handler
        val runnable = object : Runnable {
            override fun run() {
                val hours = seconds / 3600 // 计算小时
                val minutes = seconds % 3600 / 60 // 计算分钟
                val secs = seconds % 60 // 计算秒数
                val textView_time = String.format("%02d:%02d:%02d", hours, minutes, secs) // 格式化时间
                findViewById<TextView>(R.id.textView_timer).text = textView_time // 更新TextView
                if(running) {
                    seconds++ // 如果计时器在运行，秒数加一
                }
                handler.postDelayed(this, 1000) // 每秒更新一次
            }
        }
        handler.post(runnable) // 启动Runnable
    }

    override fun onSaveInstanceState(outState: Bundle) {
        Log.d("life cycle", "onSaveInstanceState") // 打印生命周期日志
        super.onSaveInstanceState(outState)
        outState.putInt("seconds", seconds) // 保存秒数
        outState.putBoolean("running", running) // 保存运行状态
        outState.putBoolean("wasRunning", wasRunning) // 保存之前的运行状态
    }

    override fun onDestroy() {
        Log.d("life cycle", "onDestroy") // 打印生命周期日志
        super.onDestroy()
    }

    override fun onStart() {
        Log.d("life cycle", "onStart") // 打印生命周期日志
        super.onStart()
    }

    override fun onStop() {
        Log.d("life cycle", "onStop") // 打印生命周期日志
        super.onStop()
    }

    override fun onResume() {
        super.onResume()
        Log.d("life cycle", "onResume") // 打印生命周期日志
        if(wasRunning) {
            running = true // 如果之前在运行，继续运行
        }
    }

    override fun onPause() {
        Log.d("life cycle", "onPause") // 打印生命周期日志
        super.onPause()
        wasRunning = running // 保存运行状态
        running = false // 暂停计时
    }
}